/*
 * @Author: chenyn 1826650603@qq.com
 * @Date: 2023-11-13 08:51:03
 * @LastEditors: neon_chen 1826650603@qq.com
 * @LastEditTime: 2024-02-20 08:48:52
 * @FilePath: \smart-campus-platform-front-vue\vue.config.js
 * @Description: 这是默认设置,请设置`customMade`, 打开koroFileHeader查看配置 进行设置: https://github.com/OBKoro1/koro1FileHeader/wiki/%E9%85%8D%E7%BD%AE
 */
const path = require("path");
var webpack = require("webpack");
const UglifyJsPlugin = require('uglifyjs-webpack-plugin')
const projectConfig = require('./public/js/config');

//获取本地ip
const os = require('os')
const getIpAddress = () => {
    let interfaces = os.networkInterfaces()

    for (let dev in interfaces) {
        let iface = interfaces[dev]

        for (let i = 0; i < iface.length; i++) {
            let {
                family,
                address,
                internal
            } = iface[i]

            if (family === 'IPv4' && address !== '127.0.0.1' && !internal) {

                return address
            }
        }
    }
}

console.log('getIpAddress: ', getIpAddress(), process.env.NODE_ENV);


let plugins
if (process.env.NODE_ENV === "production") {
    plugins = [
        new webpack.ProvidePlugin({
            favicon: path.resolve("favicon.ico") //页面标题小图标路径
        }),
        new UglifyJsPlugin({
            uglifyOptions: {
                compress: {
                    warnings: false,
                    drop_console: true, //注释console
                    drop_debugger: true, //注释debugger
                    pure_funcs: ['console.log'], //移除console.log
                },
            },
        })

    ]
} else {
    plugins = [
        new webpack.ProvidePlugin({
            favicon: path.resolve("favicon.ico") //页面标题小图标路径
        })
    ]
}

function assetsPath(_path) {
    const assetsSubDirectory = process.env.NODE_ENV === 'production' ?
        'static' :
        'static'
    return path.posix.join(assetsSubDirectory, _path)
}

module.exports = {
    // baseUrl: './', //vueConf.baseUrl, // 根域上下文目录
    outputDir: "dist", // 构建输出目录
    // assetsDir: 'assets', // 静态资源目录 (js, css, img, fonts)
    lintOnSave: true, // 是否开启eslint保存检测，有效值：ture | false | 'error'
    runtimeCompiler: true, // 运行时版本是否需要编译
    transpileDependencies: [], // 默认babel-loader忽略mode_modules，这里可增加例外的依赖包名
    productionSourceMap: false, // 是否在构建生产包时生成 sourceMap 文件，false将提高构建速度
    css: {
        // 配置高于chainWebpack中关于css loader的配置
        // modules: true, // 是否开启支持‘foo.module.css’样式
        // extract: true, // 是否使用css分离插件 ExtractTextPlugin，采用独立样式文件载入，不采用<style>方式内联至html文件中
        sourceMap: false, // 是否在构建样式地图，false将提高构建速度
        loaderOptions: {
            // css预设器配置项
            sass: {
                data: "" //`@import "@/assets/scss/mixin.scss";`
            }
        }
    },
    parallel: require("os").cpus().length > 1, // 构建时开启多进程处理babel编译
    pluginOptions: {

    },
    configureWebpack: {
        plugins,
        module: {
            rules: [{
                test: /\.swf$/, //此处添加视频组件的.swf文件编译
                loader: 'url-loader',
                options: {
                    limit: 10000,
                    name: assetsPath('fonrs/[name].[hash:7].[ext]')
                }
            }]
        },
    },
    pwa: {
        // 单页插件相关配置 https://github.com/vuejs/vue-cli/tree/dev/packages/%40vue/cli-plugin-pwa
    },
    devServer: {
        open: true,
        host: getIpAddress(),
        port: projectConfig.WebPort,
        https: false,
        hotOnly: true,
        before: app => {},
        proxy: {
            '/api': {
                target: projectConfig.ServerHost,
                ws: true, // 是否启用websockets
                changeOrigin: true, //开启代理：在本地会创建一个虚拟服务端，然后发送请求的数据，并同时接收请求的数据，这样服务端和服务端进行数据的交互就不会有跨域问题
                pathRewrite: {
                    '^/api': ''
                }
            }
        },
    }
};